@extends('backstage.layout.master')
@section('title', '优惠活动范围')
@section('style')
        <!-- page specific plugin styles -->
<link rel="stylesheet" href="/assets/css/jquery-ui-1.10.3.custom.min.css"/>
<link rel="stylesheet" href="/assets/css/chosen.css"/>
<!-- fonts -->
<style type="text/css">
    input.button {
        padding: 5px 15px 5px;
        margin: 2px 2px;
        border: none;
        background: #24a0d6;
        color: #FFF;
        cursor: pointer;
    }
    #fav_num{
        width: 20%;
    }
    #form-field-select-2 {
        width: 50%;
        margin-right: 6%;
    }
</style>
@stop
@section('content')
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>

        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a target="_blank" href="backstage/home">主页</a>
            </li>

            <li class="active">
                <a target="_blank" href="/backstage/gift/gift-list">赠品 </a>
           </li>
            <li>
                <a target="_blank" href="/backstage/gift/add-gift">添加赠品</a>
            </li>
        </ul><!-- .breadcrumb -->
    </div>

    <div class="page-content">
        <div class="row">
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                <div class="page-header">
                    <h1>
                        赠品
                        <small>
                            <i class="icon-double-angle-right"></i>
                            添加赠品
                        </small>
                    </h1>
                </div>
                <div class="row">
                    <div class="col-xs-12">
                        <!-- PAGE CONTENT BEGINS -->
                        <div class="space-4"></div>
                        <form class="form-horizontal" role="form" action= "/backstage/gift/gift-use-range-details" method="post" enctype="multipart/form-data">
                            <div class="form-group {{$errors->first('gift_id')!=""?'has-error':''}}" >
                                <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 赠品Id</label>

                                <div class="col-sm-4">
                                    <input type="text" name="gift_id" class="width-100" readonly value="{{isset($gift->id)?$gift->id:''}}">
                                    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
                                </div>
                                <div class="col-sm-5">
                                    <div class="help-block col-xs-12 col-sm-reset inline" id="gift_id_warn"> {{$errors->first('gift_id')}}</div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="form-group {{$errors->first('name')!=""?'has-error':''}}" >
                                <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 赠品名</label>

                                <div class="col-sm-4">
                                    <input type="text" readonly class="width-100"  value="{{isset($gift->name)?$gift->name:''}}">
                                </div>
                                <div class="col-sm-5">
                                    <div class="help-block col-xs-12 col-sm-reset inline" id="name_warn"> {{$errors->first('name')}}</div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="form-group ">
                             <label class="col-sm-3 control-label no-padding-right" for="form-field-1">使用范围</label>
                            <div class="col-sm-4">
                             <select name="range_type" class="form-control" id="form-field-select-1">
                              @if(isset($fav_gifts[0])&&($fav_gifts[0]->type==0))
                               <option selected value=0>全部商品</option>
                               <option  value=3>品牌</option>
                              @else
                               <option  value=0>全部商品</option>
                               <option selected value=3>品牌</option>
                               @endif
                               <option value=1>产品模板</option>
                               <option value=2>产品</option>
                             </select>
                             <div id="range-div">
                              @foreach($fav_gifts as $fav_gift)
                               @if($fav_gift->type==1)
                                <input name="product_temp_ids[]" type="checkbox" value='{{isset($fav_gift->reg_id)?$fav_gift->reg_id:'0'}}' checked="checked" />{{isset($fav_gift->res_name)?$fav_gift->res_name:''}}(产品模板)</br>
                               @elseif($fav_gift->type==2)
                               <input name="product_ids[]" type="checkbox" value='{{isset($fav_gift->reg_id)?$fav_gift->reg_id:'0'}}' checked="checked" />{{isset($fav_gift->res_name)?$fav_gift->res_name:''}}（产品）</br>
                               @elseif($fav_gift->type==3)
                               <input name="brand_ids[]" type="checkbox" value='{{isset($fav_gift->reg_id)?$fav_gift->reg_id:'0'}}' checked="checked" />{{isset($fav_gift->res_name)?$fav_gift->res_name:''}}(品牌)</br>
                               @endif
                              @endforeach
                             </div>
                            </div>
                            <div class="col-sm-5">
                                    <div class="help-block col-xs-12 col-sm-reset inline"> </div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="form-group quota {{$errors->first('quota')!=""?'has-error':''}}" @if(!isset($fav_gifts[0])||($fav_gifts[0]->type!=0)) style="display: none" @endif>
                                <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 满额送</label>

                                <div class="col-sm-4">
                                    <input type="number" name="quota"  min=0 step="0.01" value="{{isset($fav_gift->quota)?$fav_gift->quota:0}}">
                                </div>
                                <div class="col-sm-5">
                                    <div class="help-block col-xs-12 col-sm-reset inline" id="quota_warn"> {{$errors->first('quota')}}</div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div @if(isset($fav_gifts[0])&&($fav_gifts[0]->type==0)) style="display:none;" @endif class="form-group  prod_list {{$errors->first('prod_list')!=""?'has-error':''}}" >
                              <label class="col-sm-3 control-label no-padding-right" for="form-field-1">优惠对象选择</label>
                              <div class="col-xs-2">
                              <input type="text" name="keyword1" value="" placeholder="关键字" class="width-100" id="keyword1">
                               </div>
                              <div class="col-sm-5">
                               <input  type="button" value="搜索" class="button" id="fav_search">
                               <select name="result" id="result" onchange="resultChange(this.options,this.options.selectedIndex)"></select>
                               <input type="button" name="add_range" value="+" class="button" onclick="addRange()" />
                               <div class="help-block col-xs-12 col-sm-reset inline" id="prod_list_warn"> {{$errors->first('prod_list')}}</div>
                               </div>
                            </div>
                            <div class="clearfix form-actions">
                                <div class="col-md-offset-3 col-md-9">
                                    <button class="btn btn-info" type="submit">
                                        <i class="icon-ok bigger-110"></i>
                                        保存
                                    </button>
                            </div>
                            </div>
                        </form>
                    </div><!-- /.col -->
                </div>
                <!-- PAGE CONTENT ENDS -->
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.page-content -->
@stop
@section('script')
    <script type="text/javascript">
        _token = '{{csrf_token()}}';
        $(function(){
            /**
             * 优惠范围
             */
            $("select[name=range_type]").change(function(){
                var range_type=$(this).val();
                $("#result").html('');
                if(range_type!=0){
                    $(".prod_list").css('display','block');
                    $('.quota').css('display','none');
                }else{
                    $(".prod_list").css('display','none');
                    $('#range-div').html('');
                    $('.quota').css('display','block');
                }
            });
            /***
             * 搜索优惠范围
             */
        $('#fav_search').click(function () {
            var keyword1=$("#keyword1").val();
            var range_type=$("select[name=range_type]").val();
            $.ajax({
                    type: "POST",
                    url: "/backstage/gift/res-search",
                    data: {'keyword':keyword1,'range_type':range_type},
                    success: function (msg) {
                        console.log(msg.data);
                        if(msg.state='M00000'){
                            var html='';
                            if(range_type==1){
                             for(var a=0;a<msg['data'].length;a++) {
                               html+='<option value="' + msg['data'][a].res_id + '">'+ msg['data'][a].name +'</option><br />';
                             }
                            }else if(range_type==2){
                              for(var a=0;a<msg['data'].length;a++) {
                                 html+='<option value="' + msg['data'][a].res_id + '">'+ msg['data'][a].name +'</option><br />';
                              }
                            }else if(range_type==3){
                              for(var a=0;a<msg['data'].length;a++) {
                                 html+='<option value="' + msg['data'][a].res_id + '">'+ msg['data'][a].name +'</option><br />';
                              }
                            }
                            $("#result").html(html);
                        }
                    }, error: function () {
                        swal({
                            title: "系统繁忙！",
                            text: "",
                            type: "error",
                            timer:1000
                        })
                    }
                });
        });
        });
        /***
         * 去除字符串前后空格
         * @param s
         * @returns {void|XML|string}
         */
        function trim(s){
            return s.replace(/(^\s*)|(\s*$)/g, "");
        }
        /***
         * 添加范围
         */
        function addRange(id)
        {
          var selText=$("#result option:selected").text();
          var selVal=$("#result option:selected").val();
            var flag=true;
            $("#range-div input").each(function (){
                var val = $(this).val();
                if(val==selVal){
                 flag=false;
                }
            });
            var range_type=$("select[name=range_type]").val();
            if(flag){
                if(range_type==1){
                    $('#range-div').append('<input name="product_temp_ids[]" type="checkbox" value="'+selVal+'" checked="checked" />'+selText+'</br>');
                }else if(range_type==2){
                    $('#range-div').append('<input name="product_ids[]" type="checkbox" value="'+selVal+'" checked="checked" />'+selText+'</br>');
                }else if(range_type==3){
                    $('#range-div').append('<input name="brand_ids[]" type="checkbox" value="'+selVal+'" checked="checked" />'+selText+'</br>');
                }
            }
        }
        function resultChange(options,index) {
            for(var i=0; i<options.length; i++){
                if(i==index){
                    options[i].selected = true;
                }else{
                    options[i].selected = false;
                }
            }
        }
    </script>
@stop